package com.purong.purong.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.purong.purong.R;
import com.purong.purong.bean.QuestionBean;
import com.purong.purong.widget.JustifyTextView;

/**
 * Created by zengwenjie on 2017/12/6.
 */
public class HelpQuestionAdapter extends HeadRecycleAdapter<QuestionBean,HelpQuestionAdapter.ViewHolder> {
    private OnQuestionListener mListener;
    public void setOnQuestionListener(OnQuestionListener listener){
        this.mListener=listener;
    }
    @Override
    public RecyclerView.ViewHolder onCreateView(ViewGroup parent) {
        View  v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.help_question_item, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindView(ViewHolder mHolder, QuestionBean data, final int position) {
        if (data.getQuestion_content()!=null){
            int p=data.getQuestion_content().lastIndexOf("\\n");
            if (p!=-1){
                mHolder.title.setText(data.getQuestion_content().substring(0,p));
            }else {
                mHolder.title.setText(data.getQuestion_content());
            }
        }
        if (includHead()){
            if (position==getItemCount()-2){
                mHolder.cutLine.setVisibility(View.INVISIBLE);
            }else {
                mHolder.cutLine.setVisibility(View.VISIBLE);
            }
        }else {
            if (position==getItemCount()-1){
                mHolder.cutLine.setVisibility(View.INVISIBLE);
            }else {
                mHolder.cutLine.setVisibility(View.VISIBLE);
            }
        }

        mHolder.mainLay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener!=null){
                    mListener.onClick(position);
                }
            }
        });
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        //绑定每个CircleItem的控件引用到ViewHolder
        private TextView title;
        private View cutLine;
        private LinearLayout mainLay;
        public ViewHolder(View v) {
            super(v);
            cutLine=(View)v.findViewById(R.id.cut_line) ;
            title=(TextView)v.findViewById(R.id.title);
            mainLay=(LinearLayout)v.findViewById(R.id.main_lay);
        }
    }
    public interface OnQuestionListener{
        void onClick(int position);
    }
}
